package com.pkk.cloud.support.admin.controller.interior.urls;

import com.pkk.cloud.support.admin.dto.req.urls.OauthResourceUrlCheckReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.OauthUrlIdReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.OauthUrlResourceIdReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.OauthUrlsAddReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.OauthUrlsDelReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.OauthUrlsUpdateReqDto;
import com.pkk.cloud.support.admin.dto.req.urls.ResourceUrlBindReqData;
import com.pkk.cloud.support.admin.dto.resp.urls.OauthResourceUrlsCheckRespDto;
import com.pkk.cloud.support.admin.dto.resp.urls.OauthUrlsAddRespDto;
import com.pkk.cloud.support.admin.dto.resp.urls.OauthUrlsAllRespDto;
import com.pkk.cloud.support.admin.entity.resource.PenguinSysUrl;
import com.pkk.cloud.support.admin.service.PenguinSysUrlService;
import com.pkk.cloud.support.oauth2.client.aspect.annotation.PreAuth;
import com.pkk.cloud.support.oauth2.common.utils.SecurityUtils;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 接口管理控制器
 * @author: peikunkun
 * @create: 2020-04-02 14:56
 **/
@Slf4j
@RestController
@RequestMapping("/oauth/urls")
public class OauthUrlsController {


  @Resource
  private PenguinSysUrlService penguinSysUrlService;

  /**
   * @Description: 添加接口
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:57
   */
  @PreAuth
  @PostMapping("add")
  public CommonResponse<OauthUrlsAddRespDto> add(@Validated @RequestBody CommonRequest<OauthUrlsAddReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    return ResponseUtil.success(penguinSysUrlService.saveSysUrl(request.getBody(), userId));
  }


  /**
   * @Description: 删除接口操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("del")
  public CommonResponse<Boolean> del(@Validated @RequestBody CommonRequest<OauthUrlsDelReqDto> request) {
    return ResponseUtil.success(penguinSysUrlService.delSysUrlById(request.getBody().getId()));
  }

  /**
   * @Description: 更新接口操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("update")
  public CommonResponse<Boolean> update(@Validated @RequestBody CommonRequest<OauthUrlsUpdateReqDto> request) {
    final PenguinSysUrl url = new PenguinSysUrl();
    BeanUtils.copyProperties(request.getBody(), url);
    url.setUpdateTime(new Date());
    url.setUpdateName(SecurityUtils.getUserId());
    return ResponseUtil.success(penguinSysUrlService.updateSysUrlById(url));
  }


  /**
   * @Description: 获取接口所有列表操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("all")
  public CommonResponse<List<OauthUrlsAllRespDto>> all() {
    return ResponseUtil.success(penguinSysUrlService.findSysUrlList());
  }


  /**
   * @Description: 获取接口信息依据id
   * @return: com.pkk.components.rpc.response.CommonResponse<com.pkk.cloud.support.admin.dto.resp.urls.OauthUrlsAllReqDto>
   * @Author: peikunkun
   * @Date: 2020/4/11 0011 下午 5:36
   */
  @PreAuth
  @PostMapping("getUrlById")
  public CommonResponse<OauthUrlsAllRespDto> getUrlById(
      @Validated @RequestBody CommonRequest<OauthUrlIdReqDto> request) {
    final PenguinSysUrl url = penguinSysUrlService.getById(request.getBody().getId());
    final OauthUrlsAllRespDto oauthUrlsAllRespDto = new OauthUrlsAllRespDto();
    BeanUtils.copyProperties(url, oauthUrlsAllRespDto);
    return ResponseUtil.success(oauthUrlsAllRespDto);
  }


  /**
   * @Description: 根据资源id资源查询所拥有的接口信息
   * @Author: peikunkun
   * @Date: 2020/4/11 0011 下午 3:06
   */
  @PreAuth
  @PostMapping("findUrlsByResourceId")
  public CommonResponse<List<OauthUrlsAllRespDto>> findUrlsByResourceId(
      @Validated @RequestBody CommonRequest<OauthUrlResourceIdReqDto> request) {
    return ResponseUtil.success(penguinSysUrlService.findUrlsByResourceId(request.getBody().getResourceId()));
  }

  /**
   * @Description: 获取绑定的资源接口列表
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/22 0022 上午 10:24
   */
  @PreAuth
  @PostMapping("getResourceBindUrlList")
  public CommonResponse<List<OauthResourceUrlsCheckRespDto>> getResourceBindUrlList(
      @Validated @RequestBody CommonRequest<OauthResourceUrlCheckReqDto> request) {
    return ResponseUtil.success(penguinSysUrlService.getResourceBindUrlList(request.getBody()));
  }


  /**
   * @Description: 绑定的资源与接口的关系
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/22 0022 上午 10:24
   */
  @PreAuth
  @PostMapping("bindResourceUrl")
  public CommonResponse<Boolean> bindResourceUrl(
      @Validated @RequestBody CommonRequest<ResourceUrlBindReqData> request) {
    return ResponseUtil.success(penguinSysUrlService.bindResourceUrl(request.getBody()));
  }


}
